const fse = require("fs-extra");
const { recursionFilePath } = require("./util");
const { execFile } = require("child_process");

const targetPath = "../build";
const astcExePath = "astcenc-sse2.exe"
recursionFilePath(targetPath, (filePath) => {
    if (!filePath.endsWith(".png") && !filePath.endsWith(".jpg")) return;

    const astcPath = filePath.replace(".png", ".astc").replace(".jpg", ".astc");
    if (fse.existsSync(astcPath)) return;

    console.log(`${astcPath} not found, start convert`);
    try {
        execFile(astcExePath, ["-cl", filePath, astcPath, "6x6", "-medium", "-silent"], (error, stdout, stderr) => {
            if (error) {
                console.log(`error: ${error.message}`);
                return;
            }

            if (stderr) {
                console.log(`stderr: ${stderr}`);
                return;
            }

            console.log(`convert ${filePath} to ${astcPath} success!`);
        });
    } catch (error) {
        console.error(`convert ${filePath} to ${astcPath} failed!`, error);
    }
});

